﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace com.iot.core.util
{
    public class ByteUtil
    {
        public static string ByteToHex(byte[] buff)
        {
            var s = BitConverter.ToString(buff, 0, buff.Length);
            return s.Replace("-", " ");
        }
        public static byte[] HexstrToByte(string hexString) {
            hexString = hexString.Replace(" ", "");
            if ((hexString.Length % 2) != 0)
                hexString += " ";
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            return returnBytes;
        }


        public static string ByteToBinary(byte[] buff)
        {
            var s = "";
            for (var i = 0; i < buff.Length; i++)
            {
                s += Convert.ToString(buff[i], 2).PadLeft(8, '0');
            }
            return s;
        }
        public static string ByteToBinary(byte[] buff, int offset)
        {
            var s = "";
            for (var i = offset; i < buff.Length; i++)
            {
                s += Convert.ToString(buff[i], 2).PadLeft(8, '0');
            }
            return s;
        }
        public static string ByteToBinary(byte[] buff, int offset, int len)
        {
            var s = "";
            for (var i = offset; i < offset + len; i++)
            {
                s += Convert.ToString(buff[i], 2).PadLeft(8, '0');
            }
            return s;
        }


    }
}
